package com.lz.football.module.explore
{
	import com.common.utils.Reflection;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ExploreEvent;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.LButton;
	
	/**
	 * 探索地图 
	 * @author Administrator
	 * 
	 */	
	public class ExploreMapView implements IComponent
	{
		/**
		 *美洲点坐标 
		 */
		private var americaPointList:Array = [[216,219],
			[126,274],
			[147,290],
			[177,314],
			[208,332],
			[205,362],
			[206,448],
			[206,468],
			[229,426],
			[264,378],
			[237,333],
			[218,210],
			[62,183]];
		
		/**
		 *欧洲点坐标 
		 */
		private var europePointList:Array = [[297,240],
			[291,260],
			[291,152],
			[308,135],
			[322,172],
			[350,200],
			[350,219],
			[384,224],
			[430,209],
			[389,193],
			[374,156],
			[399,104],
			[478,109],
			[427,132],
			[451,166]];
		
		/**
		 *亚洲点坐标 
		 */
		private var asiaPointList:Array = [[583,392],
			[612,377],
			[598,323],
			[561,317],
			[581,295],
			[578,256],
			[626,268],
			[605,267],
			[500,311],
			[554,318],
			[548,348],
			[568,358],
			[633,354],
			[637,416],
			[668,211],
			[451,291],
			[466,243]];
		
		/**
		 *非洲点坐标 
		 */
		private var africaPointList:Array = [[365,273],
			[344,276],
			[330,317],
			[367,333],
			[379,357],
			[382,382],
			[396,415],
			[419,376],
			[434,339],
			[417,308],
			[398,292],
			[440,333]];
		
		
		private var airPoint:Array = [[873,169],[613,331],[226,147],[595,60]];
		
		
		
		/**
		 * 
		 */
		public  static var _skin:MovieClip;
		
		/**
		 *美洲 
		 */
		private var americaBtn:LButton;
		/**
		 *欧洲 
		 */
		private var europeBtn:LButton;
		/**
		 *亚洲 
		 */
		public static var asiaBtn:LButton;
		/**
		 *非洲 
		 */
		private var africaBtn:LButton;
		
		/**
		 *美洲  
		 */
		private var americaEff:MovieClip;
		/**
		 *欧洲 
		 */
		private var europeEff:MovieClip;
		/**
		 *亚洲 
		 */
		private var asiaEff:MovieClip;
		/**
		 *非洲 
		 */
		private var africaEff:MovieClip;
		private var _callBack:Function;
		private var _type:int;
		
		/**
		 *探索类型 
		 */
		private var _exploreType:int;
		
		/**
		 *探索动画播放次数  
		 *  第三次开始出现球星
		 */
		private var _effPlayTime:int;
		
		/**
		 *显示球员的数量 
		 */
		public var showTime:int;
		
		/**
		 *飞机 
		 */
		private var _air:MovieClip;
		
		public function ExploreMapView()
		{
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = view as MovieClip;
			americaBtn = new LButton(_skin.americaBtn);
			americaBtn.enable = true;
			
			europeBtn = new LButton(_skin.europeBtn);
			europeBtn.enable = true;
			asiaBtn = new LButton(_skin.asiaBtn);;
			asiaBtn.enable = true;
			africaBtn = new LButton(_skin.africaBtn);
			africaBtn.enable = true;
			_skin.americaBtn.color.gotoAndStop(4);
			_skin.europeBtn.color.gotoAndStop(4);
			_skin.asiaBtn.color.gotoAndStop(4);
			_skin.africaBtn.color.gotoAndStop(4);
			
			americaEff = _skin.americaEff;
			europeEff = _skin.europeEff
			asiaEff = _skin.asiaEff
			africaEff = _skin.africaEff;	
			_skin.removeChild(americaEff);
			_skin.removeChild(europeEff);
			_skin.removeChild(asiaEff);
			_skin.removeChild(africaEff);
			americaEff.gotoAndStop(1);
			europeEff.gotoAndStop(1);
			asiaEff.gotoAndStop(1);
			africaEff.gotoAndStop(1);
			setButtonvisible();
			addEvtListener();
			_air = Reflection.createInstance("com.lz.modul.explore.air");
			
			initAirPoint();
		}
		
		/**
		 *初始化飞机坐标 
		 * 
		 */
		public function initAirPoint():void
		{
			
			_air.rotationY = 0;
			if(AppData.getInstance().clubDatamanager.clubInfo.country == ClubConst.COUNTRY_CHAIN)//中国
			{
				_air.x = 873;
				_air.y = 169;
				_air.rotationY = 0;
			}else//其他国家的飞机坐标
			{
				_air.x = 595;
				_air.y = 60;
//				_air.rotationY = 180;
			}
//			_skin.addChild(_air);
		}
		
		public function airMove():void
		{
			initAirPoint();
			
			var arr:Array;
			switch(type)
			{
				case ClubConst.ASIA:
					arr = airPoint[0];
					break;
				case ClubConst.AFRICA:
					arr = airPoint[1];
					break;
				case ClubConst.AMERICA:
					arr = airPoint[2];
					break;
				case ClubConst.EUROPE:
					arr = airPoint[3];
					break;
			}
			if(AppData.getInstance().clubDatamanager.clubInfo.country != ClubConst.COUNTRY_CHAIN && type == ClubConst.ASIA )
			{
				_air.rotationY = 180;
				arr = [arr[0]+ 40  ,arr[1] - _air.height - 20]
			}
			
			if(type == AppData.getInstance().clubDatamanager.clubInfo.continent)
			{
				playerEffer();
			}
			else
			{
				_skin.addChild(_air);
				TweenLite.to(_air,0.5,{x:arr[0],y:arr[1],onComplete:playerEffer});
			}
		}
		
		
		
		
		
		
		
		
		
		
		/**
		 *设置按钮不可见 
		 * 
		 */
		private function setButtonvisible():void
		{
//			americaBtn.visible = false;
			_skin.americaBtn.alpha = 0;
			_skin.europeBtn.alpha = 0;
			_skin.asiaBtn.alpha = 0;;
			_skin.africaBtn.alpha = 0;
		}
		/**
		 *探索等级 
		 * @param $lv
		 * 
		 */
		public function exploreLv($lv:int):void
		{
			setButtonvisible();
			if( $lv <7)
			{
				_skin.asiaBtn.alpha = 1;
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.asiaBtn,"探索亚洲地区球员");
				
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.africaBtn,"俱乐部7级开放");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.americaBtn,"俱乐部22级开放");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.europeBtn,"俱乐部26级开放");
			}else if($lv < 18)
			{
				_skin.asiaBtn.alpha = 1;
				_skin.africaBtn.alpha = 1;
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.asiaBtn,"探索亚洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.africaBtn,"探索非洲地区球员");
				
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.americaBtn,"俱乐部18级开放");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.europeBtn,"俱乐部22级开放");
			}else if($lv < 22)
			{
				_skin.asiaBtn.alpha = 1;
				_skin.africaBtn.alpha = 1;
//				americaBtn.visible = true;
				_skin.americaBtn.alpha = 1;
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.asiaBtn,"探索亚洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.africaBtn,"探索非洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.americaBtn,"探索美洲地区球员");
				
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.europeBtn,"俱乐部22级开放");
			}else //if($lv >=22)
			{
				_skin.asiaBtn.alpha = 1;
				_skin.africaBtn.alpha = 1;
//				americaBtn.visible = true;
				_skin.americaBtn.alpha = 1
				_skin.europeBtn.alpha = 1;
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.asiaBtn,"探索亚洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.africaBtn,"探索非洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.americaBtn,"探索美洲地区球员");
				DisplayMouseEventManager.instance.addTipsDisplay(_skin.europeBtn,"探索欧洲地区球员");
			}
			
		}
		
		public function addEvtListener():void
		{
			americaBtn.addEventListener(MouseEvent.CLICK,onclickHandler);
			europeBtn.addEventListener(MouseEvent.CLICK,onclickHandler);
			asiaBtn.addEventListener(MouseEvent.CLICK,onclickHandler);
			africaBtn.addEventListener(MouseEvent.CLICK,onclickHandler);
		}
		/**
		 *1.	Austrlia&Asia
2.	America
3.	Africa
4.	Europe
 
		 * @param event
		 * 
		 */		
		protected function onclickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().exploreDataManager.exploreInfo.explorecoin <= 0 && _exploreType == 1)
			{
				Alert.showErrorTip("探索次数不足");
				return;
			}
			switch(event.target)
			{
				case americaBtn:
					if(AppData.getInstance().clubDatamanager.clubInfo.level< 18)return;
						
					type = ClubConst.AMERICA;
					break;
				case europeBtn:
					if(AppData.getInstance().clubDatamanager.clubInfo.level< 22)return;
					type = ClubConst.EUROPE;
					
					break;
				case asiaBtn:
//					if(AppData.getInstance().clubDatamanager.clubInfo.level< 2)return;
					type = ClubConst.ASIA;
					break;
				case africaBtn:
					if(AppData.getInstance().clubDatamanager.clubInfo.level< 7)return;
					type = ClubConst.AFRICA;
					
					break;
			}
			switch(_exploreType)
			{
				case 2:
					if(AppData.getInstance().clubDatamanager.clubInfo.coin < 500000)
					{
						Alert.showErrorTip("欧元不足，不可探索");
						return;
					}
					break;
				case 3:
					if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint < 20)
					{
//						Alert.showErrorTip("点券不足，不可探索");
						Alert.noCashAlert("现有点券不足20点，是否前往充值");
						return;
					}
					break;
				case 4:
					if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint < 100)
					{
//						Alert.showErrorTip("点券不足，不可探索");
						Alert.noCashAlert("现有点券不足100点，是否前往充值");
						return;
					}
					
					break;
			}
			
			
			
			
			_skin.mouseChildren = false;
			_skin.mouseEnabled = false;
			AppEventDispatcher.getInstance().dispatchEvent(new ExploreEvent(ExploreEvent.EXPLORE_PLAYER,{land:type,type:_exploreType},true));
		}
		/**
		 *播放的动画  
		 */
		private var _playerEffer:MovieClip;
		/**
		 *播放探索动画  
		 * 
		 */
		public function playerEffer():void
		{
			if(_playerEffer != null && _playerEffer.parent)_playerEffer.parent.removeChild(_playerEffer);
			switch(type)
			{
				case ClubConst.ASIA:
					_playerEffer = asiaEff
					break;
				case ClubConst.AFRICA:
					_playerEffer = africaEff
					break;
				case ClubConst.AMERICA:
					_playerEffer = americaEff
					break;
				case ClubConst.EUROPE:
					_playerEffer = europeEff
					break;
			}
			_skin.addChild(_playerEffer);
			_effPlayTime = 0;
			showTime = 0;
			_playerEffer.addFrameScript(_playerEffer.totalFrames -1,playEnd);
			_playerEffer.play();
		}
		/**
		 *获取出现点 
		 * @return 
		 * 
		 */		
		public function getappearPoint():Array
		{
			var arr:Array;
			switch(type)
			{
				case ClubConst.ASIA:
					arr = asiaPointList;
					break;
				case ClubConst.AFRICA:
					arr = africaPointList;
					break;
				case ClubConst.AMERICA:
					arr = americaPointList;
					break;
				case ClubConst.EUROPE:
					arr = europePointList;
					break;
			}
			if(arr == null)return [0,0];
			var num:int = Math.random() * arr.length;
			num = Math.floor(num);
			return arr[num];
		}
		/**
		 *停止播放探索动画  
		 * 
		 */
		public function stopEffer():void
		{
			if(_playerEffer.parent)_playerEffer.parent.removeChild(_playerEffer);
			_playerEffer.gotoAndStop(1);
			initAirPoint();
			if(_air.parent)_air.parent.removeChild(_air);
//			_skin.mouseChildren = true;
//			_skin.mouseEnabled = true;
		}
		public function setStatus():void
		{
			_skin.mouseChildren = true;
			_skin.mouseEnabled = true;
		}
		public function removeEvtListener():void
		{
			americaBtn.removeEventListener(MouseEvent.CLICK,onclickHandler);
			europeBtn.removeEventListener(MouseEvent.CLICK,onclickHandler);
			asiaBtn.removeEventListener(MouseEvent.CLICK,onclickHandler);
			africaBtn.removeEventListener(MouseEvent.CLICK,onclickHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			_callBack = null;
			if(_playerEffer != null)_playerEffer.stop();
			
			if(_air != null)TweenLite.killTweensOf(_air);
			_air = null;
			DisplayMouseEventManager.instance.removeTipsDisplay(_skin.asiaBtn);
			DisplayMouseEventManager.instance.removeTipsDisplay(_skin.africaBtn);
			DisplayMouseEventManager.instance.removeTipsDisplay(_skin.americaBtn);
			DisplayMouseEventManager.instance.removeTipsDisplay(_skin.europeBtn);
			
		}

		public function playEnd():void
		{
			_effPlayTime++;
			if(callBack != null && _effPlayTime >= 2)
				callBack();
			
		}
		/**
		 *类型 
		 */
		public function get type():int
		{
			return _type;
		}

		/**
		 * @private
		 */
		public function set type(value:int):void
		{
			_type = value;
		}

		/**
		 *播放探索动画  
		 */
		public function get callBack():Function
		{
			return _callBack;
		}

		/**
		 * @private
		 */
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}

		/**
		 *设置探索类型 
		 * @param value
		 * 
		 */
		public function set exploreType(value:int):void
		{
			_exploreType = value;
			
			_skin.americaBtn.color.gotoAndStop(value);
			_skin.europeBtn.color.gotoAndStop(value);
			_skin.asiaBtn.color.gotoAndStop(value);
			_skin.africaBtn.color.gotoAndStop(value);
		}
		
		public function get exploreType():int
		{
			return _exploreType
		}

	}
}